var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');
import Notify from '../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    code: '',
    loginErrorCount: 0
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  accountLogin: function() {
    var that = this;
  
    if(this.data.username.length < 1){
      wx.showModal({
        title: '提示',
        content: '请输入用户名',
        showCancel: false
      });
      return false;
    }
    if (this.data.password.length < 1 ) {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.LoginAccount,
      data: {
        username: that.data.username,
        password: that.data.password
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.data.errno == 0) {

          that.setData({
            loginErrorCount: 0
          });
          console.log(res)
          
          let xpasswd=res.data.data.x;
          let userInfo=res.data.data.userInfo
          let token=res.data.data.token
          if(xpasswd==1){
            Dialog.alert({
              title: '提示',
              message: '用户密码等级过低哟?',
            }).then(() => {
              app.globalData.hasLogin = true;
              wx.setStorageSync('userInfo',userInfo );
              wx.setStorageSync('hasLogin', true)
              wx.setStorage({
                key: "token",
                data: token,
                success: function() {
                  // 暂时跳转到me页面
                  wx.switchTab({
                    url: '/pages/me/me/me'
                  });
                }
              });
            })
          }else{
            app.globalData.hasLogin = true;
              wx.setStorageSync('userInfo',userInfo );
              wx.setStorageSync('hasLogin', true)
              wx.setStorage({
                key: "token",
                data: token,
                success: function() {
                  // 暂时跳转到me页面
                  wx.switchTab({
                    url: '/pages/me/me/me'
                  });
                }
              });
          }
          
          
        } else {
          that.setData({
            loginErrorCount: that.data.loginErrorCount + 1
          });
          app.globalData.hasLogin = false;
          util.showErrorToast('用户名或密码错误');
        }
      }
    });
  },
  bindUsernameInput: function(e) {

    this.setData({
      username: e.detail.value
    });
  },
  bindPasswordInput: function(e) {

    this.setData({
      password: e.detail.value
    });
  },
  bindCodeInput: function(e) {

    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})